#include <stdio.h>
#include <stdlib.h>


/*
	i=0		i=1		i=2		i=3		i=4		i=5		i=6		i=7
	n=0		n=1		n=2		n=3		n=4		n=5		n=6		n=7
	0,0		1,0		2,0		3,0		4,0		5,0		6,0		7,0
	0,1		1,1		2,1		3,1		4,1		5,1		6,1		7,1
    0,2		1,2		2,2		3,2		4,2		5,2		6,2		7,2
	0,3		1,3		2,3		3,3		4,3		5,3		6,3		7,3
    0,4		1,4		2,4		3,4		4,4		5,4		6,4		7,4
    0,5		1,5		2,5		3,5		4,5		5,5		6,5		7,5
    0,6		1,6		2,6		3,6		4,6		5,6		6,6		7,6
    0,7		1,7		2,7		3,7		4,7		5,7		6,7		7,7

    duijiaoxian compare example :

    0,4
		    1,5
    n - i = 1 - 0 = 1
    q[i] - q[n] = 4 - 5 = abs(-1) = 1, 
 */

#define max 8

int queen[max], sum = 1;

void show()
{
    int i;

    printf("The %d method:\n\n", sum);
    for (i = 0; i < max; i++) 
        printf("(%d %d) ", i, queen[i]);

    printf("\n\n");
    sum++;
}

// 检查当前列能否放置皇后
int check(int n)
{
    int i;

    for (i = 0; i < n; i++) {	// 检查横排和对角线上是否可以放置皇后
        printf("\t\t\t\t\t\t\tqueen[i=%d] = %d, queen[n=%d] = %d, abs(queen[i]-queen[n]) = %d, (n-i) = %d\n", 
			i, queen[i], n, queen[n], abs(queen[i] - queen[n]), n-i);
        if (queen[i] == queen[n] || abs(queen[i] - queen[n]) == (n - i)) {
            return 1;
        }
    }

    return 0;
}

// 回溯尝试皇后位置,n为横坐标
void put(int n)
{
    int i;

    printf("1th - level : %d Address: %p\n", n, &n);
    for (i = 0; i < max; i++) {
        queen[n] = i;		// 将皇后摆到当前循球到的位置

        printf("\tqueen[n=%d] = %d, i = %d\n", n, queen[n], i);
        if (!check(n)) {
            if (n == max - 1) {
                show();		// 如果全部摆好, 则输出所有皇后的坐标
            }
            else {
                put(n + 1);	// 否则继续摆放下一个皇后
            }
        }
    }
    printf("2th - level : %d Address: %p\n", n, &n);
}

int main()
{
    put(0);  // 从横坐标为0开始依次尝试
    printf("%d\n", sum);

    return 0;
}


/*

1th - level : 0 Address: 0xbfdf44b0
	queen[n=0] = 0, i = 0  // queen[n=0] = 0
1th - level : 1 Address: 0xbfdf4480
	queen[n=1] = 0, i = 0
							queen[i=0] = 0, queen[n=1] = 0, abs(queen[i]-queen[n]) = 0, (n-i) = 1     // (0,0） (1,0)
	queen[n=1] = 1, i = 1 // 对角线
							queen[i=0] = 0, queen[n=1] = 1, abs(queen[i]-queen[n]) = 1, (n-i) = 1     // (0,1)  (1,1)
	queen[n=1] = 2, i = 2 // queen[n=1] = 2
							queen[i=0] = 0, queen[n=1] = 2, abs(queen[i]-queen[n]) = 2, (n-i) = 1     // (0,0)  (1,2)
1th - level : 2 Address: 0xbfdf4450
	queen[n=2] = 0, i = 0
							queen[i=0] = 0, queen[n=2] = 0, abs(queen[i]-queen[n]) = 0, (n-i) = 2    // (0,0) (2,0)
	queen[n=2] = 1, i = 1
							queen[i=0] = 0, queen[n=2] = 1, abs(queen[i]-queen[n]) = 1, (n-i) = 2    // (0,0) (2,1)
							queen[i=1] = 2, queen[n=2] = 1, abs(queen[i]-queen[n]) = 1, (n-i) = 1    // (1,2) (2,1)
	queen[n=2] = 2, i = 2
							queen[i=0] = 0, queen[n=2] = 2, abs(queen[i]-queen[n]) = 2, (n-i) = 2    // (0,0) (2,2)
	queen[n=2] = 3, i = 3
							queen[i=0] = 0, queen[n=2] = 3, abs(queen[i]-queen[n]) = 3, (n-i) = 2    // (0,0) (2,3)
							queen[i=1] = 2, queen[n=2] = 3, abs(queen[i]-queen[n]) = 1, (n-i) = 1    // (1,2) (2,3)
	queen[n=2] = 4, i = 4 // queen[n=2] = 4
							queen[i=0] = 0, queen[n=2] = 4, abs(queen[i]-queen[n]) = 4, (n-i) = 2    // (0,0) (2,4)
							queen[i=1] = 2, queen[n=2] = 4, abs(queen[i]-queen[n]) = 2, (n-i) = 1    // (1,2) (2,4)
1th - level : 3 Address: 0xbfdf4420
	queen[n=3] = 0, i = 0
							queen[i=0] = 0, queen[n=3] = 0, abs(queen[i]-queen[n]) = 0, (n-i) = 3    // (0,0) (3,0）
	queen[n=3] = 1, i = 1 // queen[n=3] = 1
							 queen[i=0] = 0, queen[n=3] = 1, abs(queen[i]-queen[n]) = 1, (n-i) = 3   // (0,0) (3,1)
							 queen[i=1] = 2, queen[n=3] = 1, abs(queen[i]-queen[n]) = 1, (n-i) = 2   // (1,2) (3,1)
							 queen[i=2] = 4, queen[n=3] = 1, abs(queen[i]-queen[n]) = 3, (n-i) = 1   // (2,4) (3,1)
*/
